﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.Calendar = function(element) {
    YUIAspNet.Calendar.initializeBase(this, [element]);
}

YUIAspNet.Calendar.prototype = {
    initialize: function() {
        YUIAspNet.Calendar.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function(sender, args) {
        this._format = "MM/dd/yyyy";
        this._navConfig = null;

        if (this.get_Navigator()) {
            this._navConfig = {
                strings: {
                    month: "Choose Month",
                    year: "Enter Year",
                    submit: "OK",
                    cancel: "Cancel",
                    invalidYear: "Please enter a valid year"
                },
                monthFormat: YAHOO.widget.Calendar.SHORT,
                initialFocus: "year"
            };
        }

        var calSettings =
            {
                MULTI_SELECT: this.get_MultiSelect(),
                navigator: this._navConfig,
                close: this.get_ShowClose()
            };



        if (this.get_MinDate())
            calSettings.mindate = this._dateAsString(this.get_MinDate());

        if (this.get_MaxDate())
            calSettings.maxdate = this._dateAsString(this.get_MaxDate());

        if (this.get_SelectedDates() !== null && this.get_SelectedDates().length > 0)
            calSettings.pagedate = this.get_SelectedDates()[0];

        this._yuiControl = new YAHOO.widget.Calendar(this.get_element().id, this.get_CalendarContainer(), calSettings);

        // user localization?
        if (this.get_Localization()) {
            this._yuiControl.cfg.setProperty("LOCALE_MONTHS", "long");
            this._yuiControl.cfg.setProperty("LOCALE_WEEKDAYS", "medium");

            this._yuiControl.cfg.setProperty("MONTHS_SHORT", this.get_AbbreviatedMonthNames());
            this._yuiControl.cfg.setProperty("MONTHS_LONG", this.get_MonthNames());
            this._yuiControl.cfg.setProperty("WEEKDAYS_1CHAR", this.get_AbbreviatedDayNames());
            this._yuiControl.cfg.setProperty("WEEKDAYS_SHORT", this.get_AbbreviatedDayNames());
            this._yuiControl.cfg.setProperty("WEEKDAYS_MEDIUM", this.get_AbbreviatedDayNames());
            this._yuiControl.cfg.setProperty("WEEKDAYS_LONG", this.get_DayNames());
        }

        if (this.get_SelectedDates() !== null && this.get_SelectedDates().length > 0) {
            this._yuiControl.select(this.get_SelectedDates());
            this._syncDates(this.get_SelectedDates());
        }

        //attach events...
        var sender = this;
        var args = { cancel: false };
        
        var selectHandler = null;
        var unselectHandler = null;

        var defaultHandler = function(type, yuiArgs, obj) {
            sender._syncDates();
            if (sender.get_AutoPostback()) {
                sender.DoPostback('');
            }
        };

        if (this._events) {
            var userSelectHandler = this.get_events().getHandler("OnSelect");

            if (userSelectHandler) {
                selectHandler = function(type, yuiArgs, obj) {
                    sender._syncDates();
                    args.eventName = "OnSelect";
                    var dateArr = yuiArgs[0][yuiArgs[0].length - 1];

                    args.date = new Date(dateArr[0], dateArr[1] - 1, dateArr[2]);
                    userSelectHandler(sender, args);

                    if (sender.get_AutoPostback() && !args.cancel) {
                        sender.DoPostback('');
                    }
                };
            }

            var userUnselectHandler = this.get_events().getHandler("OnUnselect");

            if (userUnselectHandler) {
                unselectHandler = function(type, yuiArgs, obj) {
                    sender._syncDates();
                    args.eventName = "OnUnselect";
                    var dateArr = yuiArgs[0]; // one date unselected at a time.

                    args.date = new Date(dateArr[0], dateArr[1] - 1, dateArr[2]);
                    userUnselectHandler(sender, args);

                    if (sender.get_AutoPostback() && !args.cancel) {
                        sender.DoPostback('');
                    }
                };
            }
        }

        if (selectHandler == null)
            selectHandler = defaultHandler;

        if (unselectHandler == null)
            unselectHandler = defaultHandler;

        this._yuiControl.selectEvent.subscribe(selectHandler, this._yuiControl, true);
        this._yuiControl.deselectEvent.subscribe(unselectHandler, this._yuiControl, true);

        this._yuiControl.render();
    },

    _dateAsString: function(date) {
        return (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
    },

    Toggle: function() {
        if ($get(this.get_CalendarContainer()).style.display == 'none')
            this.get_YUIControl().show();
        else
            this.get_YUIControl().hide();
    },

    _syncDates: function(dateList) {

        this._persister = $get(this.get_DatePersister());
        this._persister.value = "";

        if (!dateList)
            dateList = this._yuiControl.select();

        for (var idx = 0; idx < dateList.length; idx++)
            this._persister.value += dateList[idx].format(this._format) + ",";
    },

    dispose: function() {
        //Add custom dispose actions here
        YUIAspNet.Calendar.callBaseMethod(this, 'dispose');
    },

    get_AutoPostback: function() {
        return this._autoPostback;
    },
    set_AutoPostback: function(autoPostback) {
        this._autoPostback = autoPostback;
    },

    get_ShowClose: function() {
        return this._showClose;
    },
    set_ShowClose: function(showClose) {
        this._showClose = showClose;
    },

    get_DatePersister: function() {
        return this._datePersister;
    },
    set_DatePersister: function(datePersister) {
        this._datePersister = datePersister;
    },

    get_CalendarContainer: function() {
        return this._calendarContainer;
    },
    set_CalendarContainer: function(calendarContainer) {
        this._calendarContainer = calendarContainer;
    },

    get_MultiSelect: function() {
        return this._multiSelect;
    },
    set_MultiSelect: function(multiSelect) {
        this._multiSelect = multiSelect;
    },

    get_MultiSelect: function() {
        return this._multiSelect;
    },
    set_MultiSelect: function(multiSelect) {
        this._multiSelect = multiSelect;
    },

    get_Navigator: function() {
        return this._navigator;
    },
    set_Navigator: function(navigator) {
        this._navigator = navigator;
    },

    get_SelectedDates: function() {
        return this._selectedDates;
    },
    set_SelectedDates: function(selectedDates) {
        this._selectedDates = selectedDates;
    },

    get_MinDate: function() {
        return this._minDate;
    },
    set_MinDate: function(minDate) {
        this._minDate = minDate;
    },

    get_MaxDate: function() {
        return this._maxDate;
    },
    set_MaxDate: function(maxDate) {
        this._maxDate = maxDate;
    },

    get_Localization: function() {
        return this._localization;
    },
    set_Localization: function(localization) {
        this._localization = localization;
    },

    get_DateSeparator: function() {
        return this._dateSeparator;
    },
    set_DateSeparator: function(dateSeparator) {
        this._dateSeparator = dateSeparator;
    },

    get_AbbreviatedDayNames: function() {
        return this._abbreviatedDayNames;
    },
    set_AbbreviatedDayNames: function(abbreviatedDayNames) {
        this._abbreviatedDayNames = abbreviatedDayNames;
    },

    get_DayNames: function() {
        return this._dayNames;
    },
    set_DayNames: function(dayNames) {
        this._dayNames = dayNames;
    },

    get_AbbreviatedMonthNames: function() {
        return this._abbreviatedMonthNames;
    },
    set_AbbreviatedMonthNames: function(abbreviatedMonthNames) {
        this._abbreviatedMonthNames = abbreviatedMonthNames;
    },

    get_MonthNames: function() {
        return this._monthNames;
    },
    set_MonthNames: function(monthNames) {
        this._monthNames = monthNames;
    },

    get_DatePattern: function() {
        return this._datePattern;
    },
    set_DatePattern: function(datePattern) {
        this._datePattern = datePattern;
    },

    get_DatePattern: function() {
        return this._datePattern;
    },
    set_DatePattern: function(datePattern) {
        this._datePattern = datePattern;
    },

    add_OnSelect: function(handler) {
        this.get_events().addHandler("OnSelect", handler);
    },
    remove_OnSelect: function(handler) {
        this.get_events().removeHandler("OnSelect", handler);
    },

    add_OnUnselect: function(handler) {
        this.get_events().addHandler("OnUnselect", handler);
    },
    remove_OnUnselect: function(handler) {
        this.get_events().removeHandler("OnUnselect", handler);
    }
}

YUIAspNet.Calendar.registerClass('YUIAspNet.Calendar', YUIAspNet.BaseControl);
